package com.zkingsoft.actions.customer;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tencent.common.RandomStringGenerator;
import com.tencent.common.Signature;
import com.tencent.service.CommonService;
import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.model.FlowCz;
import com.zkingsoft.model.FlowLookFy;
import com.zkingsoft.model.Fy;
import com.zkingsoft.model.Suggest;
import com.zkingsoft.model.Users;
import com.zkingsoft.model.ZdOrder;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.EncrypUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.AdvertisementColumnService;
import com.zkingsoft.services.AdvertisementPositionService;
import com.zkingsoft.services.FlowCzService;
import com.zkingsoft.services.FlowLookFyService;
import com.zkingsoft.services.FyService;
import com.zkingsoft.services.SuggestService;
import com.zkingsoft.services.UsersService;
import com.zkingsoft.services.ZdOrderService;

/**
 * 
 * @author:姜友瑶
 * @date 2016年8月31日
 */
@Controller
@RequestMapping(value = "/customer")
public class CustomerController extends BaseController {

	@Resource
	private AdvertisementPositionService adPositionService;
	@Resource
	private AdvertisementColumnService advertisementColumnService;
	@Resource
	private UsersService usersService;
	@Resource
	private FyService fyService;
	@Resource
	private FlowLookFyService flowLookFyService;

	@Resource
	private SuggestService suggestService;
	
	@Resource
	private FlowCzService flowCzService;
	@Resource
	private ZdOrderService zdOrderService;

	/**
	 * 新增建议
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addSuggest")
	public @ResponseBody AjaxResult addSuggest(Suggest suggest) {
		suggest.setCreateTime(new Date());
		suggest.setUserId(getMe().getId());
		return add(suggestService, suggest, "意见");
	}

	/**
	 * 
	 * @Description: 页面定向方法，每个权限模块公用一个，每个模块共享一个一级路径，已便于进行权限过滤
	 * @author:姜友瑶
	 * @param page1
	 * @param page2
	 * @return 返回类型 String
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/redirect/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1, @PathVariable("page2") String page2) {
		return "customer/" + page1 + "/" + page2;
	}

	@RequestMapping(value = "/redirect/{page1}")
	public String redirect(@PathVariable("page1") String page1) {
		return "customer/" + page1;
	}

	/**
	 * 修改头像
	 * 
	 * @author 姜友瑶
	 * @time 2016-08-13
	 */
	@RequestMapping(value = "/updatePhoto")
	public @ResponseBody AjaxResult updatePhoto(String photo) {

		getMe().setPhoto(photo);
		int i = usersService.modify(getMe());
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "头像修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "头像修改失败");
		}

	}

	/**
	 * 修改员工个人信息
	 */
	@RequestMapping(value = "/updateUserInfo")
	public @ResponseBody AjaxResult updateUserInfo(Users user) {
		user.setId(getMe().getId());
		int i = usersService.modify(user);
		if (i > 0) {
			// 更新session用户信息
			WebUtil.getSession().setAttribute(SystemConstance.LOGIN_KEY, usersService.findById(getMe().getId()));
			return new AjaxResult(AjaxResult.STATUS_OK, null, "个人信息修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "个人信息修改失败");
		}
	}

	/**
	 * 
	* @Description: 进入金币界面
	* @author:姜友瑶
	* @return    
	* 返回类型  String    
	* @date 2016年10月21日
	 */
	@RequestMapping(value = "/toJb")
	public String toJb() {

		//获取微信分享需要的参数
		String timestamp=String.valueOf(System.currentTimeMillis() / 1000);
		String noncestr=RandomStringGenerator.getRandomStringByLength(32);		
		//微信分享链接必须要和当前网页的连接保持一致且不能带端口号包括80【这是一个巨大的坑】
		String url=WebUtil.getCurrentPageUrl();
		String sign = "";
		try {
			sign = Signature.getJsSdkSign(CommonService.getJsapiTicket(), noncestr, timestamp, url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebUtil.getRequest().setAttribute("timestamp", timestamp);
		WebUtil.getRequest().setAttribute("noncestr", noncestr);
		WebUtil.getRequest().setAttribute("signature", sign);
		WebUtil.getRequest().setAttribute("url", url);
		return "customer/jb";
	}

	
	/**
	 * 
	* @Description: 金币充值下订单
	* @author:姜友瑶
	* @return    
	* 返回类型  String    
	* @date 2016年10月21日
	 */
	@RequestMapping(value = "/jbCz")
	public  @ResponseBody AjaxResult jbCz(Double je) {

		FlowCz cz=new FlowCz();
		cz.setContent("金币充值"+je+"元");
		cz.setCreateTime(new Date());
		//作为订单编号
		cz.setCzType("收入");
		cz.setUserId(getMe().getId());
		//作为金额
		flowCzService.add(cz);
		
		ZdOrder order=new ZdOrder();
		order.setFyId(cz.getId());
		order.setOrderNo(System.currentTimeMillis()+"");
		order.setOrderTime(new Date());
		order.setPayWay("微信");
		order.setSource("金币充值");
		order.setSpInfo("金币充值"+je+"元");
		order.setTotal(je);
		order.setUserId(getMe().getId());
		order.setYxzt("有效");
		order.setRemark("待支付");
		zdOrderService.add(order);
		//创建预付订单
		return new AjaxResult(AjaxResult.STATUS_OK,order.getOrderNo(),"充值记录添加成功");
	}

	
	
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(FlowLookFy flowLookFy, PaginationVO pageVo) {
		flowLookFy.setUserId(getMe().getId());
		pageVo.setOrder("DESC");
		pageVo.setSort("beginTime");
		return showList(flowLookFyService, flowLookFy, pageVo);
	}

	/**
	 * 用户退出系统
	 * 
	 * @param
	 * @throws @date
	 *             2015-11-30 11:35
	 * @author Matrix-J
	 */

	@RequestMapping(value = "/loginOut")
	public String loginOut(HttpSession session, HttpServletResponse response) throws Exception {
		session.invalidate();
		return "visitor/index";
	}

}